#!/bin/sh
#
# ScriptName: /etc/init.d/pureftpd
# chkconfig: - 60 50
# description: pureftpd is a ftp daemon, which is the program \
# that answers incoming ftp service requests.
# processname: pureftpd
#
# Author : cnscn
# Time : 2006-04-18 09:30
#
pureftpd_location=/usr/local/pureftpd

#tput func
funcTput()
{
   tput init
   for args
   do
   case "$args" in
        green_black)
            echo -e -n "\033[0;32m"
            ;;
        red_black)
            echo -e -n "\033[0;31m"
            ;;
        endtput)
            tput sgr0
   esac
   done
}

case "$1" in
    start)
       ps aux | grep   pure-ftp | grep -v grep > /dev/null 2>&1
       if [ $? -eq 0 ]
       then
           echo -n "Pureftpd has already been running ... "
			#funcTput red_black
			echo -e "\033[0;31m[failed]"&tput sgr0
           exit 1
       fi
       echo -n "Start pureftpd.... "
$pureftpd_location/bin/pure-config.pl $pureftpd_location/etc/pure-ftpd.conf > /dev/null 2>&1
if [ $? -eq 0 ] ; then
          #funcTput green_black
            echo -e "\033[0;32m [OK] "&tput sgr0
          #funcTput  endtput
       else
          #funcTput red_black
            echo -e "\033[0;31m [Failed] "&tput sgr0
          #funcTput  endtput
       fi
       ;;
    stop)
       ps aux | grep   pure-ftp | grep -v grep > /dev/null 2>&1
       if [ $? -eq 1 ]
       then
           echo -n "Pureftpd has not been runned now ... "
           #funcTput red_black
              echo -e "\033[0;31m [Failed] "&tput sgr0
           #funcTput endtput
           exit 1
       fi
        echo -n "Stop  pureftpd.... "
        killall pure-ftpd > /dev/null 2>&1
        if [ $? -eq 0 ] ; then
          #funcTput green_black
            echo -e "\033[0;32m [OK] "&tput sgr0
          #funcTput  endtput
        else
          #funcTput red_black
            echo -e "\033[0;31m [Failed] "&tput sgr0
          #funcTput  endtput
        fi
        ;;
    restart)
       ps aux | grep   pure-ftp | grep -v grep > /dev/null 2>&1
       if [ $? -eq 0 ]
       then
          echo -n "Stop  pureftpd.... "
          killall pure-ftpd > /dev/null 2>&1
          if [ $? -eq 0 ] ; then
             #funcTput green_black
               echo -e "\033[0;32m [OK] "&tput sgr0
             #funcTput  endtput
          else
             #funcTput red_black
               echo -e "\033[0;31m [Failed] "&tput sgr0
             #funcTput  endtput
          fi
       fi

       echo -n "Start pureftpd.... "
$pureftpd_location/bin/pure-config.pl $pureftpd_location/etc/pure-ftpd.conf >/dev/null 2>&1
 if [ $? -eq 0 ] ; then
          #funcTput green_black
             echo -e "\033[0;32m [OK] "&tput sgr0
          #funcTput  endtput
       else
          #funcTput red_black
             echo -e "\033[0;31m [Failed] "&tput sgr0
          #funcTput  endtput
       fi
       ;;
    *)
       echo "Usage: `basename $0` {start | stop | restart}"
       ;;
esac
exit 0
